# -*- coding: utf-8 -*-
import traceback, os, re, datetime, time
import pycommonlib as pyclib
from pymongo    import  Connection
from termcolor  import  cprint

#server1 = '192.168.1.150'
#server2 = 'localhost'
server1 = 'localhost'
server2 = 'beta.mana.vn'
port1   = 27017
port2   = 27017
prefix  = '/home/hoangnamhai/HarvestedData'

conn1    = Connection(server1, port1)
conn2    = Connection(server2, port2)

db1      = conn1['mobile9']
db2      = conn2['my_database']

def uploadToServer(ssh, sftp, path_dir, path_file):
    try:
        # upload to server mana.vn
        result = False
        stdin, stdout, stderr   = ssh.exec_command("mkdir -p -m 0777 {0}".format(path_dir))
        remote_file = path_file
        cprint(path_file, 'green')
        info = pyclib.rexists(sftp, remote_file)
        if not info:
            for i in range(0, 3):
                try:
                    #sftp.put(path_file, remote_file)
                    #print 'Copy file: ', path_file, ' => ', remote_file
                    os.system('rsync -v {0} daudm@mana.vn:{1}'.format(path_file, path_dir))
                    result = True; break
                except:
                   continue 
        else:
            cprint('Đã tồn tại trên server', 'red'); result = True
        return result
    except:
        traceback.print_exc()

def getAuthor(name='crawler'):
    try:
        collection    = db2['backend_user']
        result        = collection.find_one({'username': name}, {})
        if result==None: result = collection.find_one({'username': 'Hindua88'}, {})
        if result!=None:
            return result['_id']
    except:
        traceback.print_exc()

SCREEN = [
          '144x176', 
          '160x100', '160x160', '160x240', 
          '172x240',
          '176x192', '176x208', '176x220', '176x240',
          '200x400',
          '208x104', '208x208', '208x320',
          '220x220', '220x176', 
          '228x325',
          '240x240', '240x260', '240x160', '240x320', '240x350', '240x1400', '240x440', '240x480',
          '272x480', 
          '320x240', '320x256', '320x320', '320x400', 
          '360x400', '360x480', '360x640',
          '400x240', 
          '480x320', '480x400', '480x640', '480x800',
          '640x240', '640x320', '640x480', '640x960',
          '800x400', '800x480',
          '960x320', '960x480', '960x800']


def getRootLft(name='Wallpapers'):
    try:
        root_id = None; lft = None
        category    = db2['category']
        result      = category.find_one({'root_id': 143, 'data': name}, {'root_id': 1, 'lft': 1})
        if result!=None:
            root_id = result['root_id']; lft = result['lft']
        return root_id, lft
    except:
        traceback.print_exc()

LEFT = {}
for item in SCREEN:
    root_id, lft  = getRootLft(item)
    if root_id==None: cprint('Left chưa tồn tại.', 'red'); continue
    LEFT[item] = lft 
print LEFT

if __name__ == '__main__':
    try:
        cprint('start move data from local => server', 'yellow')
        collectionA = db1['article']
        collectionB = db2['article']
        #ssh     = pyclib.createSSHClient('mana.vn', 22, 'daudm', '1p%^IRg')
        ssh     = pyclib.createSSHClient('mana.vn', 22, 'daudm', '')
        sftp    = ssh.open_sftp()
        if ssh==None: cprint('Không thể kết nối tới server qua ssh.', 'red'); pyclib.forceQuit()

        result      = collectionA.find()
        for row in result:
            try:
                hashLink = row['hashLink']
                cexists = collectionB.find_one({'hashLink': hashLink})
                lft = 1
                if LEFT.has_key(row['resolution']): lft = LEFT[row['resolution']]
                else: cprint('Category chưa tồn tại (resolution)', 'red'); continue
                if cexists==None:
                    path_file = row['content']['image']['source']
                    path_file = path_file.replace('/uploads/', '')
                    subpath   = path_file[:15] 
                    path_file = '{0}/{1}'.format(prefix, path_file)
                    path_dir  = '{0}/{1}'.format(prefix, subpath)
                    image     = uploadToServer(ssh, sftp, path_dir, path_file)
                    if image==True:
                        del row['_id']
                        row['category'] = lft
                        #print row
                        collectionB.save(row)
                        cprint('Lưu thành công!', 'green')
                else: cprint('Đã tồn tại trong database.', 'red')
            except:
                traceback.print_exc()
                continue
        ssh.close()
        print 'Finished.'
    except:
        traceback.print_exc()

conn1.end_request()
conn2.end_request()
